package com.xyz.study.design_mode.q_observer.mode;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色：ConcreteSubject 具体的被观察主体
 * @author XYZ
 * @version 1.0.0
 * @since 2021-01-27 18:35
 */
public class UserChangeEvent implements Event{

    private String userId;
    private String userIdOld;

    private final List<EventListener> eventListenerList = new ArrayList<>();

    public UserChangeEvent(String userId) {
        this.userId = userId;
    }

    /**
     * {@inheritDoc}
     */
    public void addListener(EventListener eventListener) {
        if (eventListener == null) {
            return;
        }
        eventListenerList.add(eventListener);
    }

    /**
     * {@inheritDoc}
     */
    public void eventChanged(String userIdNew) {
        this.userIdOld = userId;
        this.userId = userIdNew;
        for (EventListener userChangeListener : eventListenerList) {
            userChangeListener.onEvent(this);
        }
    }

    public String getUserId() {
        return userId;
    }

    public String getUserIdOld() {
        return userIdOld;
    }
}
